package socdev.mobile;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Timer;
import java.util.TimerTask;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.StatusLine;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;


import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.TextView;
import org.json.JSONArray;
import org.json.JSONObject;

import socdev.data.JSonDownloader;
import socdev.data.Project;
import socdev.data.Task;
import socdev.data.TasksContainer;
import socdev.data.User;

public class TasksActivity extends Activity {
    private static TasksActivity context;
    private static ListView taskList;
    private static Task [] tasks;
    private static TasksContainer tasksContainer;
    
    public void onResume() {
    	super.onResume();
    	tasksContainer.update(new Handler(), new HandlerRunnable());
    }
    
    
	public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.tasks);
       
       
       context = this;
       taskList = (ListView) findViewById(R.id.task_list);
        System.out.println(taskList);
        
        User user = (User) getIntent().getExtras().get("user");
        Project project = (Project) getIntent().getExtras().get("project");
       
        tasksContainer = new TasksContainer(user, project);
        
        tasksContainer.update(new Handler(), new HandlerRunnable());

       
       Button addTask = (Button) findViewById(R.id.add_taks_button);
       addTask.setOnClickListener(new View.OnClickListener() {
           public void onClick(View view) {
               Intent myIntent = new Intent(view.getContext(), CreateTaskActivity.class);
               myIntent.putExtras(getIntent());
               startActivityForResult(myIntent, 0);
           }

       });
       
       ImageButton update = (ImageButton) findViewById(R.id.update_button);
       update.setOnClickListener(new View.OnClickListener() {
          public void onClick(View view) {
        	  tasksContainer.update(new Handler(), new HandlerRunnable());
        	  
          }

       });
       
       
       
       
    }
	
	static public class HandlerRunnable implements Runnable {
		public void run() {
				taskList.setAdapter(new TasksAdapter(context, R.layout.task_item, tasksContainer.getAll()));
		}
	}
    
   }
